package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.R;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.User;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.serviceConnection.LoginServiceConnection;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.serviceImpl.LoginService;

public class ActivityRegister extends AppCompatActivity {
    private ImageView iImageView;
    private EditText mUsername, mPassword,mPasswordAgain,mEmail;
    private Spinner sRole;
    private Button bRegister;

    // 登录香港管服务类
    private LoginServiceConnection loginConnection;

    @Override
    protected void onStart() {
        super.onStart();

        loginConnection = new LoginServiceConnection(service -> {});
        Intent intent = new Intent(this, LoginService.class);
        bindService(intent, loginConnection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (loginConnection.isBound()) {
            unbindService(loginConnection);
        }
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        iImageView = findViewById(R.id.iv_1);
        mUsername = findViewById(R.id.et_username);
        mPassword = findViewById(R.id.et_password);
        mPasswordAgain = findViewById(R.id.et_password_again);
        mEmail = findViewById(R.id.et_email);
        sRole = findViewById(R.id.sp_role);         //待修改
        bRegister = findViewById(R.id.btn_register);


        // 注册点击监听事件
        bRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = mUsername.getText().toString();
                String password =  mPassword.getText().toString();
                String passwordAgain = mPasswordAgain.getText().toString();
                String email = mEmail.getText().toString();
                Integer role = sRole.getSelectedItemPosition();

                // 输入信息不能为空
                if (username.isEmpty() || password.isEmpty() || email.isEmpty() || role == -1) {
                    Toast.makeText(ActivityRegister.this,"所有信息不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                // 正则表达式验证邮箱格式错误
                if (!isValidEmail(email)){
                    Toast.makeText(ActivityRegister.this, "邮箱格式错误", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 两次密码不一致返回
                if(!password.equals(passwordAgain)){
                    Toast.makeText(ActivityRegister.this,"两次密码不一致",Toast.LENGTH_SHORT).show();
                    return;
                }


                User user = new User(username,password,email,role);

                //注册成功与否判断
                if(loginConnection.getService().register(user)){
                    Log.d("registerDealer",""+sRole.getSelectedItemPosition());
                    Intent resultIntent = new Intent();
                    resultIntent.putExtra("RegisterInfo", true);  // 注册成功
                    setResult(RESULT_OK, resultIntent);  // 设置返回结果和数据
                }else{
                    Toast.makeText(ActivityRegister.this,"注册失败",Toast.LENGTH_SHORT).show();
                }
                finish();
            }
        });




    }

    // 验证邮箱格式的方法
    private boolean isValidEmail(String email) {
        String emailPattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$";
        return email.matches(emailPattern);
    }

}